package com.stn.zjx.givingtreasure.activity.userlogin;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.came.viewbguilib.ButtonBgUi;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.Request;
import com.orhanobut.logger.Logger;
import com.stn.zjx.givingtreasure.R;
import com.stn.zjx.givingtreasure.base.BaseActivity;
import com.stn.zjx.givingtreasure.domain.StatusDomain;
import com.stn.zjx.givingtreasure.event.NetWorkEvent;
import com.stn.zjx.givingtreasure.event.UserResetPassEvent;
import com.stn.zjx.givingtreasure.net.BaseApi;
import com.stn.zjx.givingtreasure.net.impl.UserNetImpl;
import com.stn.zjx.givingtreasure.service.UserService;
import com.stn.zjx.givingtreasure.utils.ToolsUtils;
import com.vondear.rxtools.RxNetTool;
import com.vondear.rxtools.view.RxToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class UserPassResetThreeActivity extends BaseActivity {


    @BindView(R.id.back_bt)
    RelativeLayout backBt;
    @BindView(R.id.action_title)
    TextView actionTitle;
    @BindView(R.id.password_tv)
    TextView passwordTv;
    @BindView(R.id.password_edit)
    EditText passwordEdit;
    @BindView(R.id.confirmpassword_tv)
    TextView confirmpasswordTv;
    @BindView(R.id.confirmpassword_edit)
    EditText confirmpasswordEdit;
    @BindView(R.id.ok_bt)
    ButtonBgUi okBt;
    private String phone = "";

    @Override
    public void networkStatus(NetWorkEvent netWorkEvent) {

    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_userpass_reset_three);
        ButterKnife.bind(this);
        initComponent();
        initData();
    }


    @Override
    public void initComponent() {
        actionTitle.setText(getString(R.string.resetpassword));
        phone = getIntent().getStringExtra("phone");
    }

    private void initData() {

    }


    @Override
    public boolean isSupportSwipeBack() {
        return true;
    }

    @OnClick({R.id.back_bt, R.id.ok_bt})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_bt:
                finish();
                break;
            case R.id.ok_bt:
                if (!resetCheck()) {
                    return;
                }
                resetPassword(phone, passwordEdit.getText().toString().trim());
                break;

        }
    }

    //重置密码检查
    public boolean resetCheck() {
        if (TextUtils.isEmpty(passwordEdit.getText().toString().trim())) {

            showToast(getString(R.string.password_isnotempty));
            return false;
        }
        if (!ToolsUtils.checkUserPassword(passwordEdit.getText().toString().trim())) {

            showToast(getString(R.string.password_validate));
            return false;
        }
        if (TextUtils.isEmpty(confirmpasswordEdit.getText().toString().trim())) {

            showToast(getString(R.string.confirmpassword_isnotempty));
            return false;
        }
        if (!TextUtils.equals(passwordEdit.getText().toString().trim(),
                confirmpasswordEdit.getText().toString().trim())) {

            showToast(getString(R.string.password_confirm_validate));
            return false;
        }
        return true;
    }

    //重置手机号密码
    public void resetPassword(String userPhone, String userPwd) {
        if (!RxNetTool.isAvailable(this)) {

            showToast(getString(R.string.request_networkavailable));
            return;
        }
        UserNetImpl.user_modfiy_phonepassword(userPhone, userPwd, "user_modfiy_phonepassword", new StringCallback() {
            @Override
            public void onStart(Request<String, ? extends Request> request) {
                super.onStart(request);
                showLoading();
            }

            @Override
            public void onSuccess(Response<String> response) {
                if (response.isSuccessful()) {
                    if (response.body() == null) {

                        showToast(response.message());
                        return;
                    }

                    Gson gson = new Gson();
                    StatusDomain domain = gson.fromJson(response.body().trim(), StatusDomain.class);
                    switch (domain.getStatus()) {
                        case BaseApi.REQUEST_SUCCESS_CODE:
                            showToast(domain.getMessage());
                            UserService.getInstance(UserPassResetThreeActivity.this)
                                    .exitLogin();
                            EventBus.getDefault()
                                    .post(new UserResetPassEvent());
                            break;
                        default:
                            showToast(domain.getMessage());
                            break;
                    }
                } else {
                    showToast(response.message());

                }
            }

            @Override
            public void onError(Response<String> response) {
                super.onError(response);
                showToast(getString(R.string.request_please_wait));

            }

            @Override
            public void onFinish() {
                super.onFinish();
                if (!isFinishing()) {
                    dissmissLoading();
                }
            }
        });
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void UserResetPassEvent(UserResetPassEvent event) {
        finish();
    }


}
